package com.box.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

/**
 * 淘淘商城自定义响应结构
 */
public class JsonUtils {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
    	try {
    		Gson gson = new Gson();
			String result = gson.toJson(data);
			String string = MAPPER.writeValueAsString(result);
			return string;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
    
    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     * @param data
     * @return
     */
    public static String objectToJson2(Object data,HttpServletResponse response,Type listType) {
    	//如果是Struts2这里可以使用ServletActionContext.getResponse();获取HttpServletResponse对象
    			Gson gson = new Gson();
    			String result = gson.toJson(data);
    			response.setContentType("text/json; charset=utf-8");
    			response.setHeader("Cache-Control", "no-cache"); // 取消浏览器缓存
    			PrintWriter out = null;
    			try {
    				out = response.getWriter();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			out.print(result);
    			out.flush();
    			out.close();
    			return null;
    }
    
    
    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     * @param data
     * @return
     */
    public static String objectToJson3(Object data,HttpServletResponse response) {
    	//如果是Struts2这里可以使用ServletActionContext.getResponse();获取HttpServletResponse对象
    			Gson gson = new Gson();
    			String result = gson.toJson(data);
    			response.setContentType("text/json; charset=utf-8");
    			response.setHeader("Cache-Control", "no-cache"); // 取消浏览器缓存
    			PrintWriter out = null;
    			try {
    				out = response.getWriter();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			out.print(result);
    			out.flush();
    			out.close();
    			return null;
    }
    
    /**
     * 将json结果集转化为对象
     * 
     * @param jsonData json数据
     * @param clazz 对象中的object类型
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            T t = MAPPER.readValue(jsonData, beanType);
            return t;
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
    	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
    	try {
    		List<T> list = MAPPER.readValue(jsonData, javaType);
    		return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	return null;
    }
    
}
